package cn.itcast.bos.before.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.serializer.FilterUtils;

import cn.itcast.bos.domain.delivery.Promotion;
import cn.itcast.bos.domain.utils.PageBean;
import freemarker.template.Configuration;
import freemarker.template.Template;

@Controller
@Scope("prototype")
@Namespace("/")
@ParentPackage("json-default")
public class PromotionAction extends BaseAction<Promotion>{
	
	@Action(value="showPage",results={@Result(name="success",type="json")})
	public String showPage(){
		PageBean pageBean = WebClient.create("http://localhost:8080/bos_management/bos_mgr/bos/promotion").query("rows", rows).query("page", page)
		.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
		.get(PageBean.class);
		ServletActionContext.getContext().getValueStack().push(pageBean);
		return SUCCESS;
	}
	
	@Action(value="promotion_detail")
	public String promotion_detail() throws Exception{
		
		String realPath =ServletActionContext.getServletContext().getRealPath("/promotion");
		File file=new File(realPath+"/"+t.getId()+".html");
		if(!file.exists()){
			String ip="http://localhost:8080";
			 Promotion promotion = WebClient.create(ip+"/bos_management/promotion_service/bos/promotion/"+t.getId())
						.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
						.get(Promotion.class);
			Configuration configuration=new Configuration(Configuration.VERSION_2_3_22);
			//设置模板路径
			configuration.setDirectoryForTemplateLoading(new File(ServletActionContext.getServletContext().getRealPath("/WEB-INF/templates")));
			//获得模板文件
			Template template = configuration.getTemplate("promotion_detail.ftl");
			Map<String,Object> map=new HashMap<String, Object>();
			String description = promotion.getDescription();
			description=description.replaceAll("/bos_management", ip+"/bos_management");
			promotion.setDescription(description);
			String titleImg=promotion.getTitleImg();
			titleImg=titleImg.replaceAll("/bos_management", ip+"/bos_management");
			promotion.setTitleImg(titleImg);
			System.out.println(promotion);
			//将数据对象封装到map集合中
			map.put("promotion", promotion);
			//已流的形式将map 中的数据写到静态页面中
			template.process(map, new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
		}
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			FileUtils.copyFile(file, ServletActionContext.getResponse().getOutputStream());
		
		return NONE;
	}
}
